<?php
class HomeAction extends CommonAction{
	public $usertype;	//1:主人(自己的主页)    2:访客(已登录的)   3:游客(未登录的)
	public $id;			//主页的ID get('id');
	public $uid;		//Session 的ID
	
	//id的检测及加载用户类型检测
	public function __construct(){
		$this->uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$idres = M()->query('SELECT UserId FROM users WHERE UserId='.$this->_get('id')); 	//请修改此处查询的ID及表名
		$this->id = $idres[0]['UserId'];
		//还有此处的字段名
		if(!$this->id)
			$this->id = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		if(!$this->id)
			echo '<script>window.history.back(-1);</script>';
		$this->getusertype();
	}
	
	//判断用户类型
	public function getusertype(){
		if($this->id==$this->uid){
			$this->usertype=1;
		}else{
			if($this->uid)
				$this->usertype=2;
			else
				$this->usertype=3;
		}
		$usertype = $this->usertype;
		$this->assign('usertype',$usertype);
	}

	//教官首页
	public function index(){
		$getuid=$this->_get('id');	//userid
		$vid=$this->_get('ActiveViewIndex');
		$Model= new Model();
		$user=$Model->query("SELECT U.UserId,U.SchoolId,U.TrueName,U.OrgName,U.Avatar,U.LastLoginDate,U.UserType,U.IsJury,T.DepartmentName,T.Title,T.QQ FROM users U,teacher T WHERE U.UserId = T.UserId and T.UserId = {$getuid}");
		$this->assign('uid',$user['0']['UserId']);	//主页链接传参用
		$this->assign('QQ',$user['0']['QQ']);		
		$this->assign('user',$user);
		$this->assign('usertype',$this->usertype);
		$this->display();
	}
	//基本信息
	public function editBaseInfo(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		if(!$this->_post('submit')){
			$user=M('users')->where("UserId={$uid}")->field('TrueName,PhoneNumber,Email')->find();
			$this->assign('user',$user);		
			$this->display();
		}else{
			$data=array(
				"TrueName" => "{$this->_post('TrueName')}",
				"PhoneNumber" => "{$this->_post('PhoneNumber')}",
				"Email" => "{$this->_post('Email')}",
			);
			edit_base_info($uid,$data);
			//$this->redirect("/Officer/Home/editBaseInfo/id/{$uid}");
			$this->success('修改基本信息成功','editBaseInfo',1);
		}
	}
	//详细信息
	public function editDetailinfo(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		if(!$this->_post('submit')){
			$user = M('teacher')->where("UserId={$uid}")->find();
			$this->assign('user',$user);
			$this->display('editDetailInfo');
		}else{
			$data=array(
				"DepartmentName" => "{$this->_post('DepartmentName')}",
				"Job" => "{$this->_post('Job')}",
				"Address" => "{$this->_post('Address')}",
				"PostalCode" => "{$this->_post('PostalCode')}",
				"FixedPhone" => "{$this->_post('FixedPhone')}",
				"OtherPhone" => "{$this->_post('OtherPhone')}",
				"MSN" => "{$this->_post('MSN')}",
				"QQ" => "{$this->_post('QQ')}",
				"Fetion" => "{$this->_post('Fetion')}",
				"PersonalIntroduce" => "{$this->_post('PersonalIntroduce')}",
			);
			edit_detail_info($uid,$data);
			$this->success('修改详细信息成功','editDetailinfo',1);
		}
	}
	//修改头像
	public function editAvatar(){
		$this->display();
	}
	
	public function editAvatarAjax(){
			$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$OldAvatar = M('Users')->where("UserId={$uid}")->field('Avatar')->find();
			import('ORG.Net.UploadFile');
			import("ORG.Util.Image");
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 1048576 ;// 设置附件上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置附件上传类型
			$upload->savePath = './UpFile/Img/Avatar/';// 设置附件上传目录
			$upload->thumb = true; 
			//设置需要生成缩略图的文件后缀 
			$upload->thumbPrefix = 's_';
			//设置缩略图最大宽度
			$upload->thumbMaxWidth = '165';
			//设置缩略图最大高度
			$upload->thumbMaxHeight = '165';
			//删除原图
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()) {
				// 上传错误提示错误信息
				$this->error($upload->getErrorMsg());
			}else{
				// 上传成功 获取上传文件信息
				$info = $upload->getUploadFileInfo();
				$Avatar = 's_'.$info[0]['savename'];
				edit_avatar($uid,$Avatar);
			} 
		}
	//修改密码
	public function editPassword(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
	 	if(!$this->_post("new_pass")){
			//加载修改界面
			$this->display('editPassword');
		}else{
			//判断原密码是否匹配
		 	$m = M('users')->where("UserId={$uid}")->find();
			$oldpass=md5(trim($this->_post('old_pass')));
			if($m['Password'] != $oldpass){
				$this->error('原始密码错误','editPassword',1);//跳转 or 弹窗?
			}
			if($this->_post('new_pass') != $this->_post('check_pass')){
				$this->error('两次密码不一致','editPassword',1);
			}else{
				$newpass=md5(trim($this->_post('new_pass')));
				edit_pass($uid,$newpass);
				$this->redirect("/Officer/Home/index/id/{$uid}");
			}  
		}
	}
/* 	//添加学习资源
    public function StudyResources(){
		$this->display();
    }		
	public function AddStudyResources(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$study = D('Studyresources');
		$result = $study->create();
		if (!$result){
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			exit($study->getError());
		}else{
			// 验证通过 可以进行其他数据操作			
			$arr['UserName'] = $_SESSION[C('USER_AUTH_KEY')]['UserName'];
			$arr['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$arr['Title'] = $this->_post('Title');
			$arr['Category'] = $this->_post('Category');
			$arr['Content'] = $this->_post('Content');
			$arr['Source'] = $this->_post('Source');
			$arr['CreateDate'] = date("Y-m-d H:i:s");
			add_study_resources($arr);
			$this->redirect("/Officer/Home/index/id/{$uid}");
		}
	} */
	
	//编辑学习资源
	public function editStudyResources(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$id=$this->_get('id');	//resources ID
		if(!$this->_post('Send')){
			$m = M('studyresources')->where("id={$id}")->find();
			$this->assign('info',$m);
			$this->display();
		}else{			
			$m = M('studyresources');			
			$id=$this->_post('resourcesid');
			$data['Title']=$this->_post('Title');
			$data['Content']=$this->_post('Content');
			$data['Source']=$this->_post('Source');
			$data['Category']=$this->_post('Category');
			$m->where("Id={$id}")->save($data);
			$this->redirect("/Officer/Home/index/id/{$uid}"); 		
		}
	}
	
	//添加博文
	public function addBlog(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		if(!$this->_post('Submit')){
			$this->display();
		}else{			
			$arr['Title'] = $this->_post('Title');
			$arr['Tags'] = $this->_post('Tags');
			$arr['Content'] = $this->_post('Content',false);
			$arr['CreateDate'] = date("Y-m-d H:i:s");
			$arr['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$arr['TrueName'] = $_SESSION[C('USER_AUTH_KEY')]['TrueName'];
			add_blog($arr);
			$this->redirect("/Officer/Home/index/id/{$uid}"); 
		}
	}
	
	//编辑博文
	public function editBlog(){
		$id=$this->_get('id');	//blogID
		if(!$this->_post('Submit')){
			$m = M('bloglog')->where("id={$id}")->find();
			$this->assign('info',$m);
			$this->display();
		}else{
			$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$m = M('bloglog');
			$id=$this->_post('blogid');
			$data['Title']=$this->_post('Title');
			$data['Content']=$this->_post('Content');
			$data['Tags']=$this->_post('Tags');
			$m->where("Id={$id}")->save($data);
			$this->redirect("/Officer/Home/index/id/{$uid}"); 
		}		
	}
	//删除博文
	public function delBlog(){
		$id=$this->_get('id');	//blogID
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$m = M('bloglog');
		$data['IsDeleted']='1';	//0显示 1删除
		$m->where("Id={$id}")->save($data);
		$this->redirect("/Officer/Home/index/id/{$uid}"); 
	}
	
	//添加学习资源
	public function addBusinessProblems(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
	 	if(!$this->_post('btnsubmint')){
			$this->display('addBusinessProblems');
		}else{
			$m = M('users')->where("UserId={$uid}")->field("OrgName,TrueName")->find(); 			//查询出题企业和创建人
			$orgnameid = M('enterprise')->where("Name='".$m['OrgName']."'")->field("Id")->find();	//查询出题企业id
			$time=date('Y-m-d H:m:s');
		   	$data=array(
				"Title" => "{$this->_post('Title')}",
				"EnterpriseId" => "{$orgnameid['Id']}",
				"Goal" => "{$this->_post('Goal')}",
				"Description" => "{$this->_post('Description')}",
				"Support" => "{$this->_post('Support')}",
				"Advise" => "{$this->_post('Advise')}",
				"CreateDate" => "{$time}",
				"CreateUser" => "{$m['TrueName']}",
				"IsPublic" => "1",		//不公开0,公开1.
				"IsApproved" => "0"	//默认值0,审核通过1.  
			);   
			add_business_problems($data); 
			echo '<script>alert("添加成功,请等待专家小组的审核!");window.location.href="./"</script>';
		}
	}
	
	//个人主页选项卡
	public function a0(){
		$type=$this->_get('type');
		@$page = $this->_get('page');
		if(!intval($page))
			$page=1;
		$info = new InfoModel($this->_get('id'),$page,$type);
		if(!$info->uid)
			echo '<script>alert("非法操作");window.history.back(-1);</script>';
		$this->assign('uid',$info->uid);				
		$this->assign('list',$info->list);				
		$this->assign('pagenum',$info->pagenum);		//总页数
		$this->assign('page',$info->page);				//当前页
		switch($type){
			case 0:
				$this->display('a0');					//我的点评
			break;
			case 1:
				$this->display('a1');					//我的博文
			break;			
			case 2:
				$this->display('a2');					//学习资源
			break;
		}
	}
}